var Sign = {
	
	errorDisplay: $('p.pass-generalErrorWrapper'),
	
	errorContent : $('.pass-generalError-error'),
	
	userName : $('.pass-text-input-userName'),

	userNameClearBtn : $('.pass-clearbtn-userName'),
	
	userNameWrapper: $('#userNameWrapper'),
	
	password : $('.pass-text-input-password'),
	
	passwordClearBtn : $('.pass-clearbtn-password'),
	
	passwordWrapper: $('#userPwdWrapper'),
	
	imageCode : $('.pass-text-input-verifyCode'),
	
	imageCodeClearBtn: $('.pass-clearbtn-verifyCode'),
	
	tryAnotherBtn : $('.pass-change-verifyCode'),
	
	verifyCodeImg : $('#verifyCodeImg'),
	
	submitBtn : $('.pass-button-submit'),
	
	init: function() {
		var _this = this;
		
		_this._initLoginUI();
		
		_this.userName.keyup(function(){
			if ($(this).val()) {
				_this.userNameClearBtn.show();
			} else {
				_this.userNameClearBtn.hide();
			}
		});
		
		_this.password.keyup(function(){
			if ($(this).val()) {
				_this.passwordClearBtn.show();
			} else {
				_this.passwordClearBtn.hide();
			}
		});
		
		_this.imageCode.keyup(function(){
			if ($(this).val()) {
				_this.imageCodeClearBtn.show();
			} else {
				_this.imageCodeClearBtn.hide();
			}
		});
		
		_this.userNameClearBtn.click(function(){
			if (!$(this).is('hidden')) {
				_this.userName.val("");
				_this.userName.focus();
				$(this).hide();
			}
		});
		
		_this.passwordClearBtn.click(function(){
			if (!$(this).is('hidden')) {
				_this.password.val("");
				_this.password.focus();
				$(this).hide();
			}
		});
		
		_this.imageCodeClearBtn.click(function(){
			if (!$(this).is('hidden')) {
				_this.imageCode.val("");
				_this.imageCode.focus();
				$(this).hide();
			}
		});
		
		_this.tryAnotherBtn.click(function(){
			_this.verifyCodeImg.attr("src", "geneImgCode?rnd=" + Math.random());
		});
		
		_this.submitBtn.click(function(){
			_this._submitLogin();
		});

		$('.pass-form').bind('keydown', function(event){
			var winevent = event || window.event;
			if (winevent.keyCode == 13) {
				var targetID = winevent.target.id;
				if (targetID == 'userName') {
					if (_this.checkUserName()) {
						if ($.trim(_this.password.val()) === '') {
							_this.password.focus();
							return;
						}
					}
				}
				
				if (targetID == 'password' || targetID == 'verifyCode' 
						|| targetID == 'remPwd' || targetID == 'loginSubmit') {
					_this._submitLogin();
				}
			}
		});
		
	},
	
	_submitLogin : function() {
		var _this = this;

		if (!navigator.cookieEnabled) return;
		
		if (this.checkUserName() && this.checkPwd() && this.checkCaptcha()) {
			//$('.pass-form').submit();
			/**/
			$.ajax({
				type: 'POST',
				dataType : 'json',
            	contentType: 'application/json',
				url: '/cloudils/login?type=object',
				data: JSON.stringify({userName : _this.userName.val(), password : _this.password.val()}),
				success:function(data){
					if (data.success) {
						window.location.href = "myHome"
					}
				},
				error:function(){
					alert("failure");
				}
			}); 
			
		}
	},
	
	checkUserName : function () {
		var _this = this;
		var result = false;
		
		if ($.trim(_this.userName.val()) == '') {
			_this.errorDisplay.show();
			_this.errorContent.text($('#noinput-userName').text());
			_this.userName.focus();
		} else {
			_this.errorDisplay.hide();
			result = true;
		}
		
		return result;
	},
	
	checkPwd : function () {
		var _this = this;
		var result = false;
		
		if ($.trim(_this.password.val()) == '') {
			_this.errorDisplay.show();
			_this.errorContent.text($('#noinput-passWd').text());
			_this.password.focus();
		} else {
			_this.errorDisplay.hide();
			result = true;
		}
		
		return result;
	},	
	
	checkCaptcha : function () {
		var _this = this;
		var result = false;
		
		if ($.trim(_this.imageCode.val()) == '') {
			_this.errorDisplay.show();
			_this.errorContent.text($('#noinput-imageCode').text());
			_this.imageCode.focus();
		} else {
			_this.errorDisplay.hide();
			result = true;
		}
		
		return result;
	},
	
	_initLoginUI : function() {
		var _this = this;
	
		if (_this.userName.val()) _this.userNameClearBtn.show();
		
		if (_this.password.val()) _this.passwordClearBtn.show();
		
		if (_this.imageCode.val()) _this.imageCodeClearBtn.show();
	
	}
	
};

$(document).ready(Sign.init());